package com.atguigu.spark;

import org.apache.spark.SparkConf;
import org.apache.spark.SparkContext;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import scala.Tuple2;

import java.util.Arrays;

public class Solution {
    public static void run() {
        SparkConf sparkConf = new SparkConf();
        sparkConf.setMaster("local[1]").setAppName("spark solution");
        JavaSparkContext context = JavaSparkContext.fromSparkContext(SparkContext.getOrCreate(sparkConf));
        JavaRDD<String> source = context.textFile("input");
        source.flatMap(str -> Arrays.stream(str.split(" ")).iterator())
                .mapToPair(str -> new Tuple2<>(str, 1))
                .reduceByKey(Integer::sum)
                .saveAsTextFile("output/solution");
        context.close();
    }
}
